package rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
            // 1. 建立连接
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("192.168.100.10"); // ip地址
            connectionFactory.setPort(5672);             // 端口号
            connectionFactory.setUsername("study");      // 用户名
            connectionFactory.setPassword("study");      // 用户密码
            connectionFactory.setVirtualHost("bite");    // 虚拟主机
            Connection connection = connectionFactory.newConnection();

            // 2. 开启信道
            Channel channel = connection.createChannel();

        // 3. 声明队列
        channel.queueDeclare(Constants.WORK_QUEUE, true, false, false, null);

        for (int i = 0; i < 10; i++) {
            String msg = "hello work queue..." + i;
            channel.basicPublish("", Constants.WORK_QUEUE, null, msg.getBytes());
        }

        // 4. 释放资源
        channel.close();
        connection.close();
    }
}
